<?

define ("_GLOBAL_URI_PATH", "/");
define ("_GLOBAL_ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . _GLOBAL_URI_PATH);
session_name ("PHP_SESSION_ADMIN");
session_save_path ($_SERVER["DOCUMENT_ROOT"]."/tmp");
session_cache_limiter("private, must-revalidate");
session_set_cookie_params (0, _GLOBAL_URI_PATH . _GLOBAL_SITE_PATH, "", 0);
session_start();

/* require_once ("class.db.inc");

global $_GLOBAL_DBH;

$_GLOBAL_DBH = new DB (array (
		iface=>"mysql",
		host=>"localhost",
		user=>"vlazeq",
		password=>"qvlaze",
		db=>"vlazeq"
	)); */

global $_APP_SESSION;
global $_APP_USER_MODULES;
global $_APP_WG_MODULES;
global $_APP_SESSION_USER;
global $_APP_SESSION_THEME;
	
/* create a user session if user_name and user_password are set */
if (!isset ($_SESSION["_APP_SESSION"])) {

	if (isset ($_REQUEST["user_name"]) &&
			isset ($_REQUEST["user_password"])) {

		global $_GLOBAL_DBH;

		/* get user info from users table */
		$password = crypt ($_REQUEST["user_password"],"SC");
		$user_query = $_GLOBAL_DBH->query ("SELECT * FROM users
				WHERE name='{$_REQUEST["user_name"]}'
				AND passwd='{$password}'");

		if ($user_data = $_GLOBAL_DBH->fetch_object ($user_query)) {

			$_SESSION["_APP_SESSION"] = true;
			/* set user data to global $_APP_SESSION_USER */
			$_APP_SESSION_USER = $user_data;
			$_APP_SESSION_THEME = $user_data->theme;
		}
	}
} 

/* else terminate user session is session_logout is set */
elseif (isset ($_REQUEST["session_logout"])) {

	session_unset ();
	$_APP_SESSION_THEME = "contemporary";
}

/*
 * because we're not using php sessions to store the default data
 * for the sake of reloading the user access runtime
 * we currently reselect the user data and then reset the $_APP_SESSION_USER
 */
elseif (isset ($_SESSION["_APP_SESSION"])) {
	
	$user_query = $_GLOBAL_DBH->query ("SELECT * FROM users
			WHERE id={$_SESSION["_APP_SESSION_USER"]->id}");

	if ($user_data = $_GLOBAL_DBH->fetch_object ($user_query)) {

		$_APP_SESSION_USER = $user_data;
		$_APP_SESSION_THEME = $user_data->theme;
	} else $_APP_SESSION_THEME = "contemporary";
} else $_APP_SESSION_THEME = "contemporary";


if (!$_APP_SESSION_THEME) $_APP_SESSION_THEME = "contemporary";

if (isset ($_SESSION["_APP_SESSION"])) {
	
	/* retrieve the user roles runtime */
	$roles = $_GLOBAL_DBH->query ("SELECT roles.* FROM user_roles,roles WHERE user_id={$_APP_SESSION_USER->id} AND role_id=roles.id");
	//print ("SELECT roles.* FROM user_roles,roles WHERE user_id={$_APP_SESSION_USER->id} AND role_id=roles.id;<BR>");
	
	while ($role = $_GLOBAL_DBH->fetch_object ($roles)) {
		
		/* append the $role to global $_APP_USER_ROLES array */
		$_APP_USER_ROLES[] = $role;

		/* get all the modules that role may access */
		$modules = $_GLOBAL_DBH->query ("SELECT modules.id,modules.access,modules.code FROM role_modules,modules WHERE role_id={$role->id} AND module_id=modules.id");
		//print ("SELECT modules.id,modules.access,modules.code FROM role_modules,modules WHERE role_id={$role->id} AND module_id=modules.id;<BR>");
		
		while ($module = $_GLOBAL_DBH->fetch_object ($modules)) {
		
			/* append module to $_APP_USER_MODULES by module code */
			$_APP_USER_MODULES[$module->code] = $module;
			

			/*
			 * compare what access a role may have against the access of a module.
			 * if the module access is greater then the role access then drop the
			 * role_access of the module to the role access for runtime restriction check.
			 */
			if ($role->access == "public" &&
					preg_match ("/exclusive|private/",
					$_APP_USER_MODULES[$module->code]->access))
				
				/* drop the module_role_access to public */
				$_APP_USER_MODULES[$module->code]->role_access = $role->access;
			elseif ($role->access == "private" &&
					preg_match ("/exclusive/",
					$_APP_USER_MODULES[$module->code]->access))
				
				/* drop the module_role_access to private */
				$_APP_USER_MODULES[$module->code]->role_access = $role->access;
			else 
			
				/* drop the module_role_access to exclusive */
				$_APP_USER_MODULES[$module->code]->role_access = $role->access;
			//print "SETTING THE FIRST ACCESS FOR $module->code AS $module->role_access<br>";
		}
	}

//print_r ($_APP_USER_MODULES);


	/* $workgroups = $_GLOBAL_DBH->query ("SELECT workgroups.id,workgroups.access,roles.id as role_id FROM user_roles,roles,workgroup_roles,workgroups WHERE user_id={$_APP_SESSION_USER->id}  AND user_roles.role_id=roles.id AND roles.id=workgroup_roles.role_id AND workgroup_id=workgroups.id");

	while ($workgroup = $_GLOBAL_DBH->fetch_object ($workgroups)) {

                $_APP_WG_ROLES[] = $workgroup;
                $modules = $_GLOBAL_DBH->query ("SELECT modules.id,modules.access,modules.code FROM role_modules,modules WHERE role_id={$workgroup->role_id} AND module_id=modules.id");

                while ($module = $_GLOBAL_DBH->fetch_object ($modules)) {

                        $_APP_WG_MODULES[$module->code] = $module;

                        if ($workgroup->access == "public" &&
                                        preg_match ("/exclusive|private/",
                                        $_APP_WG_MODULES[$module->code]->access))

                                $_APP_WG_MODULES[$module->code]->role_access = $workgroup->access;
                        elseif ($workgroup->access == "private" &&
                                        preg_match ("/exclusive/",
                                        $_APP_WG_MODULES[$module->code]->access))

                                $_APP_WG_MODULES[$module->code]->role_access = $workgroup->access;
                        else $_APP_WG_MODULES[$module->code]->role_access = $workgroup->access;
                }
	}; */

	//print_r ($_APP_WG_MODULES);
	//print_r ($_APP_WG_ROLES);
}

if (isset ($_SESSION["_APP_SESSION"])) {

	$data = array ();

	$data["_GET"] = $_GET;
	$data["_POST"] = $_POST;
	$data["_REQUEST"] = $_REQUEST;
	$data["_SERVER"] = $_SERVER;

	$data = mysql_escape_string (serialize ($data));

	/* user history data */
	if ((!preg_match ("/fs_active_tasks|fs_assigned_tasks|history/", $_SERVER["PHP_SELF"])) &&
			(!preg_match ("/_order=/", $_SERVER["QUERY_STRING"]))) {

		$_GLOBAL_DBH->query ("INSERT INTO history(user_id,data) VALUES ({$_APP_SESSION_USER->id},'$data')");
	}

	/* timesheet data */
	if (isset ($_REQUEST["timesheet"])) {

		if ($_REQUEST["timesheet"] == "in") {

			$_GLOBAL_DBH->query ("INSERT INTO timesheet(user_id,date_begin,date_end) VALUES ({$_APP_SESSION_USER->id},current_timestamp(),'')");
		} elseif ($_REQUEST["timesheet"] == "out") {

			$_GLOBAL_DBH->query ("UPDATE timesheet SET date_end=current_timestamp() WHERE user_id={$_APP_SESSION_USER->id} AND date_end=''");
			#$_GLOBAL_DBH->query ("UPDATE timesheet SET date_end=unix_timestamp() WHERE user_id={$_APP_SESSION_USER->id} AND id={$_REQUEST["timesheet_id"]} AND date_end=0");
			
		}
	}

	define ("_GLOBAL_USER_HOME", _GLOBAL_ROOT_PATH."usr/{$_APP_SESSION_USER->id}");
}

$_APP_SESSION = $_SESSION["_APP_SESSION"];
$_SESSION["_APP_SESSION_USER"] = $_APP_SESSION_USER;
$_SESSION["_APP_PANEL_TITLES"] = array ();

?>
